package com.yl.chainStore.control.warehouse

import com.alibaba.fastjson.JSONObject
import com.yl.chainStore.common.Constants.Name.Companion.ID
import com.yl.chainStore.common.Constants.Name.Companion.STATUS
import com.yl.chainStore.common.enums.TypeEnum
import com.yl.chainStore.control.BaseCtrl
import com.yl.chainStore.entity.warehouse.TechnologyProcessing
import com.yl.chainStore.pojo.vo.pay.PayCustomerOrderDetailSelectVo
import com.yl.chainStore.pojo.vo.warehouse.TechnologyProcessingMapperVo
import com.yl.chainStore.services.*
import com.yl.common.ErrorCode
import com.yl.common.control.ResponseBody
import com.yl.common.control.returnError
import com.yl.common.control.returnSuccess
import com.yl.common.getContext
import com.yl.users.common.annotations.PermsCode
import org.springframework.transaction.annotation.Transactional
import org.springframework.transaction.interceptor.TransactionAspectSupport
import org.springframework.web.bind.annotation.*

/**
 * @description :技加工管理
 * @author :吴枫
 * @create :2021-06-22 09:45:00
 */
@RestController
@RequestMapping("warehouse/processLog")
open class TechnologyProcessingController(
        private val iTechnologyProcessingService: ITechnologyProcessingService,
        private val webCompanyDeptService: IWebCompanyDeptService,
        private val iCustomerInfoService: ICustomerInfoService,
        private val iWebCompanyDeptService: IWebCompanyDeptService
) : BaseCtrl() {

    /**
     * 技加工记录列表(分页)
     */
    @PostMapping("processListByPage")
    @PermsCode("warehouse:process:list")
    @Transactional
    open fun processListByPage(@RequestBody vo: TechnologyProcessingMapperVo): ResponseBody {
        return returnSuccess(webCompanyDeptService.processListByPage(vo))
    }

    /**
     * 删除技加工记录
     */
    @PostMapping("processDelete/{id}")
    @Transactional(rollbackFor = [Exception::class])
    @PermsCode("warehouse:process:del")
    open fun processDelete(@PathVariable id: Long): ResponseBody {
        val result = iTechnologyProcessingService.processDelete(id)
        if (result.code != ErrorCode.SUCCESS.code) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()
        }
        return result
    }


    /**
     * 添加技加工记录
     */
    @PostMapping("processAdd")
    @PermsCode("warehouse:process:add")
    @Transactional(rollbackFor = [Exception::class])
    open fun processAdd(@RequestBody eo: TechnologyProcessing): ResponseBody {
        log.info("eo;{}", eo)
        //进行参数判断

        if (eo.customerId == null) {
            return returnError(ErrorCode.PARAM_EMPTY, "请输入客户id")
        }
        if (eo.medicalHistoryId == null) {
            return returnError(ErrorCode.PARAM_EMPTY, "请输入关联病例id")
        }

        if (eo.details.isNullOrEmpty()) {
            return returnError(ErrorCode.PARAM_EMPTY, "请填写加工物品")
        }
        if (eo.processOrderCode == null) {
            return returnError(ErrorCode.DATA_IS_NOT_FIND, "请填写订单编号")
        }
        if (eo.consigneeId == null) {
            return returnError(ErrorCode.DATA_IS_NOT_FIND, "请输入取件人id")
        }
        if (eo.companyUserId == null) {
            return returnError(ErrorCode.DATA_IS_NOT_FIND, "请输入医生id")
        }
        val result = webCompanyDeptService.processAdd(eo)
        if (result.code != ErrorCode.SUCCESS.code) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()
        }
        return result
    }



    /**
     * 修改技加工记录
     */
    @PostMapping("processUpdate")
    @PermsCode("warehouse:process:update")
    @Transactional(rollbackFor = [Exception::class])
    open fun processUpdate(@RequestBody pro: TechnologyProcessing): ResponseBody {

        if (pro.details.isNullOrEmpty()) {
            return returnError(ErrorCode.DATA_IS_NOT_FIND)
        }

        val result = iTechnologyProcessingService.processUpdate(pro)
        if (result.code != ErrorCode.SUCCESS.code) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()
        }
        return result
    }


    /**
     * 修改技加工订单状态
     */
    @PostMapping("processStatusUpdate")
    @PermsCode("warehouse:process:updateStatus")
    @Transactional(rollbackFor = [Exception::class])
    open fun processStatusUpdate(@RequestBody data: JSONObject): ResponseBody {
        //先对参数进行判断
        if (!data.containsKey(ID) && !data.containsKey(STATUS)) return returnError(ErrorCode.PARAM_EMPTY, "缺少字段")
        if (data.getLong(ID) == null || data.getString(STATUS) == null) return returnError(ErrorCode.PARAM_EMPTY, "参数为空")

        val result = iTechnologyProcessingService.processStatusUpdate(data)
        if (result.code != ErrorCode.SUCCESS.code) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()
        }
        return result
    }



    /**
     * 根据id查询技加工订单详情列表
     */
    @PostMapping("processDetail/{id}")
    @Transactional
    open fun processDetail(@PathVariable id: Long): ResponseBody {
        return returnSuccess(iTechnologyProcessingService.processDetail(id))
    }

    /**
     * 获取技加工订单状态
     */
    @GetMapping("getTechnologyProcessStatus")
    @Transactional
    open fun getRegisteredSourceType(): ResponseBody {
        val typeList = mutableListOf<Map<String, Any>>()
//        typeList.add(TypeEnum.TECHNOLOGY_PROCESSING_NEW.getMap())
        typeList.add(TypeEnum.TECHNOLOGY_PROCESSING_MAKING.getMap())
        typeList.add(TypeEnum.TECHNOLOGY_PROCESSING_MAKED.getMap())
        typeList.add(TypeEnum.TECHNOLOGY_PROCESSING_FINISHED.getMap())

        return returnSuccess(typeList)
    }

    /**
     * 技加工获取订单详情列表
     */
    @PostMapping("/technologyProcessingOrderDetail/{id}")
    @Transactional
    open fun technologyProcessingOrderDetail(@PathVariable id: Long): ResponseBody {
        val payCustomerOrderDetailSelectVo = PayCustomerOrderDetailSelectVo()
        payCustomerOrderDetailSelectVo.customerId = id

        return returnSuccess(iCustomerInfoService.getOrderDetailList(id, payCustomerOrderDetailSelectVo))
    }

    /**
     * 加工厂下拉框
     */
    @GetMapping("getTechnologyPlantList")
    @Transactional
    open fun getTechnologyPlantList(): ResponseBody {
        val companyId = getContext().companyId
        return returnSuccess(iWebCompanyDeptService.getTechnologyPlantList(companyId))
    }

    /**
     * 查询订单记录
     */
    @PostMapping("getCustomOrderList/{id}")
    @Transactional
    open fun getCustomOrderList(@PathVariable  id: Long): ResponseBody {
        return returnSuccess(iCustomerInfoService.getCustomOrderList(id))
    }



}
